package cn.jsl.product.service.impl;

import cn.jsl.basic.exception.MyException;
import cn.jsl.basic.service.impl.BaseServiceImpl;
import cn.jsl.basic.util.AjaxResult;
import cn.jsl.product.domain.Product;
import cn.jsl.product.domain.ProductDetail;
import cn.jsl.product.domain.ProductOnlineAuditLog;
import cn.jsl.product.mapper.ProductDetailMapper;
import cn.jsl.product.mapper.ProductMapper;
import cn.jsl.product.mapper.ProductOnlineAuditLogMapper;
import cn.jsl.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author phub
 * @since 2022-04-15
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper detailMapper;
    @Autowired
    private ProductOnlineAuditLogMapper logMapper;

    /**
     * 级联插入新增
     * @param product 服务信息与服务详情信息
     */
    @Override
    @Transactional
    public void save(Product product) {
        //参数校验
        if (Objects.isNull(product)){
            throw new MyException("保存服务信息参数异常!");
        }
        //初始化创建时间
        product.setCreatetime(new Date());
        productMapper.save(product);//保存服务 获取自增id

        ProductDetail detail = product.getDetail();//获取服务详情信息
        detail.setProduct_id(product.getId());//绑定对应的服务的id
        detailMapper.save(detail);//持久化

    }

    /**
     * 级联删除
     * @param id 服务的id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        //根据服务的id删除服务详情
        detailMapper.deleteByProductid(id);
        //删除服务
        productMapper.delete(id);
    }

    /**
     * 级联批量删除
     * @param ids
     */
    @Override
    public void patchDelete(List<Long> ids) {
        detailMapper.patchDeleteByProductId(ids);
        super.patchDelete(ids);
    }

    @Override
    public AjaxResult onsale(HashSet<Long> ids) {
        StringBuilder builder = new StringBuilder();
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            String[] imgsUrl = product.getResources().split(",");//获取图片地址(多个)
       /*     if (!BaiduAuditUtils.TextCensor(product.getName())) {
                builder.append("宠物名字非法!");
            } else {
                for (int i = 0; i < imgsUrl.length - 1; i++) {
                    if (!BaiduAuditUtils.ImgCensor("http://47.95.117.210:8888" + imgsUrl[i])) {//图形审核失败
                        builder.append("图形审核失败!");
                        break;
                    }
                }
            }*/
            if (builder.length()>0) {//失败
                ProductOnlineAuditLog auditLog = new ProductOnlineAuditLog();
                auditLog.setProduct_id(id);
                auditLog.setState(1);
                auditLog.setAudit_time(new Date());
                auditLog.setNote(builder.toString());
                logMapper.save(auditLog);
            } else {
                productMapper.onsale(id, new Date());
                ProductOnlineAuditLog auditLog = new ProductOnlineAuditLog();
                auditLog.setProduct_id(id);
                auditLog.setState(0);
                auditLog.setAudit_time(new Date());
                auditLog.setNote("审核成功!");
                logMapper.save(auditLog);
                builder.setLength(0);
            }
        }
        return AjaxResult.me();
    }

    @Override
    public AjaxResult offsale(HashSet<Long> ids) {
        productMapper.offsale(ids, new Date());
        return AjaxResult.me();
    }
}
